<template>
  <div class="common_wrap">
    <div class="common_title">责任人维护</div>
    <el-form
      ref="queryForm"
      :model="queryParams"
      label-width="75px"
      inline
      class="search-nav"
    >
      <el-form-item prop="indicator" label="类型">
        <el-select v-model="queryParams.type" clearable>
          <el-option
            v-for="(item, index) in typeList"
            :key="item.value + index"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item prop="indicator" label="姓名">
        <el-input
          v-model="queryParams.indicator"
          placeholder="请输入姓名"
          maxlength="16"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery"
          >查询</el-button
        >
        <el-button
          type="primary"
          plain
          icon="el-icon-refresh"
          @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <div class="btn-container" style="text-align: right">
      <el-button type="primary" icon="el-icon-plus" @click="toAdd"
        >新增</el-button
      >
    </div>
    <el-table ref="table" v-loading="tableLoading" :data="tableData">
      <el-table-column prop="level" label="类型" align="center">
        <template slot-scope="scope">{{
          formateLevel(scope.row.level)
        }}</template>
      </el-table-column>
      <el-table-column prop="empName" label="姓名" align="center" />
      <el-table-column
        prop="areaName"
        label="管辖区域"
        align="center"
        width="180"
      />
      <el-table-column prop="quotaCount" label="涉及指标" align="center" />
      <el-table-column prop="workTask" label="分管工作" align="center">
        <template slot-scope="scope">
          <div v-if="scope.row.workTask">
            {{ scope.row.workTask }}
          </div>
          <div v-else>/</div>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-tooltip content="编辑" placement="top">
            <el-button
              type="primary"
              icon="el-icon-edit"
              circle
              @click="handleClaim(scope.row)"
            />
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      class="pagination-container"
      :background="true"
      :current-page="queryParams.current"
      :page-sizes="[10, 20, 30, 40, 50]"
      :page-size="queryParams.size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="sizeChange"
      @current-change="currentChange"
    />
  </div>
</template>

<script>
export default {
  name: "PersonMaintain",
  data() {
    return {
      // 搜索条件
      queryParams: {
        type: "",
        indicator: "",
        current: 1, // 第几页
        size: 10, // 每页条数
      },
      total: 0, // 总条数
      tableLoading: false, // 表格loading
      tableData: [], // 表格数据
      typeList: [
        { label: "责任领导", value: "0" },
        { label: "主要领导", value: "1" },
        { label: "市管领导", value: "2" },
      ],
    };
  },
  computed: {
    formateLevel() {
      return function (level) {
        switch (level) {
          case 1:
            return "主要领导";
          case 2:
            return "市管领导";
          default:
            return "责任领导";
        }
      };
    },
  },
  created() {
    this.getTableData();
  },
  methods: {
    resetQuery() {
      this.queryParams = {
        type: "",
        indicator: "",
        current: 1,
        size: 10,
      };
      this.getTableData();
    },
    handleQuery() {
      this.queryParams.current = 1;
      this.getTableData();
    },
    // 每页显示数量变化
    sizeChange(size) {
      this.queryParams.size = size;
      this.queryParams.current = 1;
      this.getTableData();
    },

    // 当前页变化
    currentChange(current) {
      this.queryParams.current = current;
      this.getTableData();
    },
    getTableData() {
      this.tableLoading = true;
      const params = {
        empName: this.queryParams.indicator,
        level: this.queryParams.type,
        index: this.queryParams.current,
        limit: this.queryParams.size,
      };
      this.$api.employeeList(params).then((res) => {
        this.tableLoading = false;
        if (res.status === 200) {
          this.tableData = res.data.data.items;
          this.total = res.data.data.count;
        } else {
          this.tableData = [];
          this.total = 0;
        }
      });
    },
    toAdd() {
      this.$router.push({ path: "/personMaintain/edit" });
    },
    handleClaim(row) {
      this.$router.push({
        path: "/personMaintain/edit",
        query: {
          ...row,
        },
      });
    },
  },
};
</script>

<style scoped>
</style>